package com.movitech.mobile.cz.base.supplier.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.common.util.ListObjCopyUtil;
import com.movitech.mobile.cz.base.common.util.StringUtils;
import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import com.movitech.mobile.cz.base.supplier.entity.SSupplierFormModifyMapping;
import com.movitech.mobile.cz.base.supplier.entity.ext.SSupplierFormModifyMappingRespVO;
import com.movitech.mobile.cz.base.supplier.mapper.SSupplierFormModifyMappingMapper;
import com.movitech.mobile.cz.base.supplier.service.SSupplierFormModifyMappingService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.movitech.mobile.cz.base.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 供方表单字段新旧数据表 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-03-08
 */
@Service
public class SSupplierFormModifyMappingServiceImpl extends ServiceImpl<SSupplierFormModifyMappingMapper, SSupplierFormModifyMapping> implements SSupplierFormModifyMappingService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public RestReturnResult<Object> getBySupplierId(String supplierId,String relatedId) throws Exception {
        RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, null, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);
        if(StringUtils.isNotEmpty(supplierId)){
            EntityWrapper wrapper = new EntityWrapper<>();
            SSupplierFormModifyMapping sSupplierFormModifyMapping=new SSupplierFormModifyMapping();
            sSupplierFormModifyMapping.setSuppliersId(supplierId);
            sSupplierFormModifyMapping.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
            sSupplierFormModifyMapping.setShowFlag(CommonConstants.SHOW_FLAG);
            sSupplierFormModifyMapping.setDongFlag(CommonConstants.UNDONG_FLAG);
            wrapper.setEntity(sSupplierFormModifyMapping);
            wrapper.orderBy(" update_date ",false);
            List<SSupplierFormModifyMapping> ls=this.selectList(wrapper);
            List<SSupplierFormModifyMappingRespVO> respLs=ListObjCopyUtil.copyTo(ls, SSupplierFormModifyMappingRespVO.class);
            for(SSupplierFormModifyMappingRespVO obj:respLs){
                if(StringUtils.isNotEmpty(obj.getUpdateBy())){
                    String userName=sysUserService.getSysUserNameByIds(obj.getUpdateBy());
                    if(StringUtils.isNotEmpty(userName))obj.setUpdateByname(userName);
                }
            }
            hResult=RestReturnResult.success();
            hResult.setData(respLs);
        }else if(StringUtils.isNotEmpty(relatedId)){
            EntityWrapper wrapper = new EntityWrapper<>();
            SSupplierFormModifyMapping sSupplierFormModifyMapping=new SSupplierFormModifyMapping();
            sSupplierFormModifyMapping.setSuppliersId(supplierId);
            sSupplierFormModifyMapping.setRelatedId(relatedId);
            sSupplierFormModifyMapping.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
            sSupplierFormModifyMapping.setShowFlag(CommonConstants.SHOW_FLAG);
            wrapper.setEntity(sSupplierFormModifyMapping);
            wrapper.orderBy(" update_date ",false);
            List<SSupplierFormModifyMapping> ls=this.selectList(wrapper);
            List<SSupplierFormModifyMappingRespVO> respLs=ListObjCopyUtil.copyTo(ls, SSupplierFormModifyMappingRespVO.class);
            for(SSupplierFormModifyMappingRespVO obj:respLs){
                if(StringUtils.isNotEmpty(obj.getUpdateBy())){
                    String userName=sysUserService.getSysUserNameByIds(obj.getUpdateBy());
                    if(StringUtils.isNotEmpty(userName))obj.setUpdateByname(userName);
                }
            }
            hResult=RestReturnResult.success();
            hResult.setData(respLs);
        }else{
            hResult.setMsg("id为空");
        }
        return hResult;
    }
}
